package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Sku;
import com.itheima.service.Impl.SkuServiceImpl;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
public class SkuController {

    @Resource
    private SkuServiceImpl skuServiceImpl;

    @Resource
    private RestHighLevelClient client;

    @GetMapping("/findAll")
    public void findAll() throws IOException {
        //1.创建CreateIndexRequest对象，并指定索引库名称
        CreateIndexRequest request = new CreateIndexRequest("sku");
        //2.指定settings配置
        request.settings(Settings.builder()
                .put("index.number_of_shards",3)
                .put("index.number_of_replicas",1)
        );
        //3.指定mapping配置
        request.mapping("{\n" +
                "  \"mappings\": {\n" +
                "    \"properties\": {\n" +
                "      \"id\": {\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"name\": {\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"price\": {\n" +
                "        \"type\": \"double\"\n" +
                "      },\n" +
                "      \"num\": {\n" +
                "        \"type\": \"integer\"\n" +
                "      },\n" +
                "      \"image\": {\n" +
                "        \"type\": \"keyword\",\n" +
                "        \"index\":\"false\"\n" +
                "      },\n" +
                "      \"createTime\": {\n" +
                "        \"type\": \"date\"\n" +
                "      },\n" +
                "      \"categoryId\": {\n" +
                "        \"type\": \"integer\"\n" +
                "      },\n" +
                "      \"categoryName\": {\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"brandName\": {\n" +
                "        \"type\": \"keyword\"\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}", XContentType.JSON);
        //3.查询数据
        List<Sku> skuList = skuServiceImpl.findAll();
        //4.创建BulkRequest对象
        BulkRequest bulkRequest = new BulkRequest();
        //5.创建多个IndexRequest对象，并添加到BulkRequest中
        for (Sku sku : skuList) {
            bulkRequest.add(new IndexRequest("sku")
                    .id(sku.getId().toString())
                    .source(JSON.toJSONString(sku), XContentType.JSON)
            );
        }
        //6.发起请求
        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        //7.打印结果
        System.out.println("status: " + bulkResponse.status());
    }

}
